﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class DeliveryTraceUpdaterCreator : BaseUpdaterCreator<DeliveryTraceModel>
    {
        public DeliveryTraceUpdater CreateDeliveryTraceUpdaterInstance()
        {
            DeliveryTraceUpdater deliveryTraceUpdater = new DeliveryTraceUpdater(new DeliveryTraceFetcher());
            deliveryTraceUpdater.DataUpdating += FlagAndApproveFlagValid;
            deliveryTraceUpdater.DataUpdating += ReferExist;
            return deliveryTraceUpdater;
        }

        private void ReferExist(BaseUpdater<DeliveryTraceModel> sender, DataUpdatingEventArgs<DeliveryTraceModel> e)
        {
            if (e.Insert == null)
                return;
            var orderFetcher = new OrderFetcher();
            var orderModel= orderFetcher.Get(e.Insert.Order);
            if(orderModel==null)
            {
                e.Cancel = true;
                e.Message = String.Format("无法找到有效的订单{0}", e.Insert.Order);
                return;
            }
            if(orderModel.Retail != e.Insert.Retail)
            {
                e.Cancel = true;
                e.Message = String.Format("物流信息的经销商{0}与订单上的经销商{1}不一致", e.Insert.Retail, orderModel.Retail);
                return;
            }
        }

    }
}
